//
//  SZLWithDrawViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/25.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLWithDrawViewController.h"
#import "MBProgressHUD+MJ.h"
#import "MyCountNetwork.h"
#import "SZLAccountHelper.h"
#import "VefityTool.h"
#import "WaterRippleWait.h"

@interface SZLWithDrawViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) NSArray *titles;
@property (nonatomic,strong) NSArray *placeHolders;
@property (nonatomic,strong) NSMutableArray *values;

@end

@implementation SZLWithDrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.typeModel) {
        self.navigationItem.title = @"编辑信息";
    }else{
        self.navigationItem.title = @"新建账户";
    }
    self.titles = @[@"姓名:",@"联系电话:",@"提现类型:",@"银行卡或支付宝:"];
    self.placeHolders = @[@"请输入绑定账户的真实姓名",@"请输入绑定账户的电话号码",@"例如工商银行或支付宝",@"请输入账户"];
    if (self.typeModel) {
        self.values = [NSMutableArray arrayWithObjects:self.typeModel.name,self.typeModel.withdrawPhone,self.typeModel.typeName,self.typeModel.withdrawNumber, nil];
    }else{
        self.values = [NSMutableArray arrayWithObjects:@"",@"",@"",@"", nil];
    }
    [self setTableView];
    [self setNaviBar];
    [self addObserver];
}
- (void)setNaviBar
{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
}
- (void)setTableView
{
    self.tableView.backgroundColor = RGBColorOne(240);
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    self.tableView.rowHeight = 50;
    self.tableView.tableFooterView = [[UIView alloc]init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 30)];
    titleLab.text = self.titles[indexPath.row];
    titleLab.font = Font(15);
    titleLab.textColor = [UIColor blackColor];
    titleLab.width = [titleLab sizeThatFits:CGSizeMake(MAXFLOAT, 20)].width;
    [cell.contentView addSubview:titleLab];
    UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLab.frame)+5, 10, ScreenW-titleLab.width-20-5, 30)];
    field.font = Font(15);
    field.borderStyle = UITextBorderStyleNone;
    field.text = self.values[indexPath.row];
    field.placeholder = self.placeHolders[indexPath.row];
    field.returnKeyType = UIReturnKeyDone;
    field.delegate = self;
    field.tag = indexPath.row;
    [field addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    [cell.contentView addSubview:field];
    if (indexPath.row == 1) {
        field.keyboardType = UIKeyboardTypeNumberPad;
    }
    return cell;
}
- (void)textChanged:(UITextField *)field
{
    self.values[field.tag] = field.text;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
//提交提现请求
- (void)saveClick
{
    [self.view endEditing:YES];
    if (![self isValidValues]){
        [MBProgressHUD showError:@"信息填写不完整,请填写完整" toView:self.view];
        return;
    }
    if (![VefityTool isValidTellNumber:self.values[1]]) {
        [MBProgressHUD showError:@"请输入有效的联系电话" toView:self.view];
        return;
    }
    if (self.typeModel) {
        NSMutableDictionary *parame = [NSMutableDictionary dictionary];
        parame[@"token"] = [SZLAccountHelper shareInstance].token;
        parame[@"id"] = @(self.typeModel.ID);
        parame[@"name"] = self.values[0];
        parame[@"withdrawPhone"] = self.values[1];
        parame[@"typeName"] = self.values[2];
        parame[@"withdrawNumber"] = self.values[3];
        [WaterRippleWait waterRippleShowInView:self.view backgroundColor:[UIColor clearColor]];
        [MyCountNetwork updateDrawListsWithParame:parame response:^(BOOL success, NSString *msg) {
            [WaterRippleWait hideWaterRippleInView:self.view];
            if (success) {
                [MBProgressHUD showSuccess:@"提交申请成功" toView:self.view];
                if (self.saveBlock) {
                    self.saveBlock();
                }
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MBProgressHUD showError:msg toView:self.view];
            }
        }];
    }else{
        NSMutableDictionary *parame = [NSMutableDictionary dictionary];
        parame[@"token"] = [SZLAccountHelper shareInstance].token;
        parame[@"name"] = self.values[0];
        parame[@"withdrawPhone"] = self.values[1];
        parame[@"typeName"] = self.values[2];
        parame[@"withdrawNumber"] = self.values[3];
        [WaterRippleWait waterRippleShowInView:self.view backgroundColor:[UIColor clearColor]];
        [MyCountNetwork withDrawAddWithParame:parame response:^(BOOL success, NSString *msg) {
            [WaterRippleWait hideWaterRippleInView:self.view];
            if (success) {
                [MBProgressHUD showSuccess:@"添加成功" toView:self.view];
                if (self.saveBlock) {
                    self.saveBlock();
                }
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MBProgressHUD showError:msg toView:self.view];
            }
        }];
    }
    
}
- (BOOL)isValidValues
{
    for (NSString *str in self.values) {
        if (str.length<1) {
            return NO;
        }
    }
    return YES;
}

- (void)keyBoardFrameChanged:(NSNotification *)noti
{
    CGRect endRect = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    if (endRect.origin.y<ScreenH) {
        self.tableView.contentSize = CGSizeMake(ScreenW, ScreenH+endRect.size.height);
    }else{
        self.tableView.contentSize = CGSizeMake(ScreenW, ScreenH-64-20);
    }
}
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardFrameChanged:) name:UIKeyboardDidChangeFrameNotification object:nil];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
}

@end
